package com.rla.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.rla.common.annotation.RepeatSubmit;
import com.rla.common.annotation.Log;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.domain.PageQuery;
import com.rla.common.core.domain.R;
import com.rla.common.utils.validate.AddGroup;
import com.rla.common.utils.validate.EditGroup;
import com.rla.common.enums.BusinessType;
import com.rla.common.utils.poi.ExcelUtil;
import com.rla.domain.vo.IntegralRuleVo;
import com.rla.domain.bo.IntegralRuleBo;
import com.rla.service.IIntegralRuleService;
import com.rla.common.core.page.TableDataInfo;

/**
 * 积分规则
 *
 * @author rla
 * @date 2025-09-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/integral/rule")
public class IntegralRuleController extends BaseController {

    private final IIntegralRuleService iIntegralRuleService;

    /**
     * 查询积分规则列表
     */
    @GetMapping("/list")
    public TableDataInfo<IntegralRuleVo> list(IntegralRuleBo bo, PageQuery pageQuery) {
        return iIntegralRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出积分规则列表
     */
    @Log(title = "积分规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IntegralRuleBo bo, HttpServletResponse response) {
        List<IntegralRuleVo> list = iIntegralRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "积分规则", IntegralRuleVo.class, response);
    }

    /**
     * 获取积分规则详细信息
     *
     * @param ID 主键
     */
    @GetMapping("/{ID}")
    public R<IntegralRuleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ID) {
        return R.ok(iIntegralRuleService.queryById(ID));
    }

    /**
     * 新增积分规则
     */
    @Log(title = "积分规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IntegralRuleBo bo) {
        return toAjax(iIntegralRuleService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改积分规则
     */
    @Log(title = "积分规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IntegralRuleBo bo) {
        return toAjax(iIntegralRuleService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除积分规则
     *
     * @param IDs 主键串
     */
    @Log(title = "积分规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] IDs) {
        return toAjax(iIntegralRuleService.deleteWithValidByIds(Arrays.asList(IDs), true) ? 1 : 0);
    }
}
